package ru.ifmo.ipimenov.ewf_svn.standalone.dao.impl;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import ru.ifmo.ipimenov.ewf_svn.standalone.dao.UserDao;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.db.User;
import ru.ifmo.ipimenov.ewf_svn.standalone.model.db.Document;
import ru.ifmo.ipimenov.ewf_svn.standalone.util.HibernateUtil;

import java.util.List;

/**
 * @author: Ilya Pimenov
 * Scartel Starlab company
 */
public class UserDaoHibernate implements UserDao {
    /**
     * Logger for all purposes
     */
    private static Log log = LogFactory.getLog(UserDaoHibernate.class);

    public User load(String username) {
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        s.beginTransaction();

        User u = (User)s.createCriteria(User.class).add(Restrictions.eq("name", username)).uniqueResult();
        s.getTransaction().commit();

        return u;

    }

    public void save(User u) {
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        s.beginTransaction();

        s.save(u);

        s.getTransaction().commit();
    }
}
